<?php

/** soListTable class.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *  @author Jyry Kuukkanen
 *  @copyright Jyry Kuukkanen
*/

include_once("sodebug.php");
include_once("sostring.php");
include_once("solist.php");

/* Define the list table types */
define("SO_LIST_TABLE_BUTTON", 1); /* Chooce single item and commit. Default */
define("SO_LIST_TABLE_CHECK", 2);  /* Chooce multiple items */
define("SO_LIST_TABLE_RADIO", 3);  /* Chooce single item. Commit button
                                      separately */

/** A list table class.
 *  Create html code for a chooce list. List can be of following types single,
 *  many and radio. After setting the values for the properties a chooce list
 *  can be created. The html code for the list table can be read from the
 *  ListHtml property.
 *
 *  When the form is submited where the list table is located a variable named
 *  listTable is sent with the form. Its value is the row index that was
 *  chosen. Index is zero based
 *  @var bool $Centered Flag to indicate whether the list table should be
 *      centered
 *  @var string $Width Width of the field (percent or pixel value)
 *  @var int $Default Applies with radio buttons. States which line is selected
 *      by default. NOTE: line number is one based.
 *  @var array/int $__WidgetTypes List of component types
 *  @var array/int $__Caption Captions for widgets
 *  @var string $__ListHtml Produced html code
 *  @var soList $__Rows Data for the cells in the table
 *  @var int $__ColCount Number of columns
 *  @var array/string $__Titles Titles for the column. "*" is special tag.
 *      It means set components to this column ??? XXX Henkka, what is this?
 *  @var array/int $__WidgetCols The numnber of the column that has the widget
 *  @var array/string $__BgColors background color codes for elements
 *       (th, td, etc)
 *  @package solisttable
 */
class soListTable {
    var $Centered = false;
    var $Width;
    var $Default;
    var $__WidgetTypes;
    var $__ListHtml = "";
    var $__Rows;
    var $__ColCount; /*int Number of columns */
    var $__Titles;
    var $__WidgetCols;
    var $__BgColors;


    /** Constructor
     *  @package solisttable
     */
    function soListTable() {
        $this->Type = SO_LIST_TABLE_BUTTON;
        $this->Center = false;
        $this->reset();
    } // soListTable


    /** Reset all properties
     *  @package solisttable
     */
    function reset() {
        $this->__Titles = array();
        $this->__Rows = new soList();
        $this->__ColCount = 0;
        $this->Width = "50%";
        $this->__ListHtml = "";
        $this->Default = -1;
    } // reset


    /** Sets a background color for an element
     *  @param string $Element Element name to set bg color for
     *  @param string $Color Color code
     */
    function setBgColor($Element, $Color) {
        if (!$Element) return; // <-- EXIT
        $this->__BgColors[strtolower($Element)] = $Color;
    } // setBgColor

    /** Set widget types
     *  XXX Henkka, what does this do?
     *  @param array/int $Widgets Widget types. 
     *  @param array/string $Captions Widget captions
     */
    function setWidgets($Widgets, $Captions = array()) {
        $this->__WidgetTypes = $Widgets;
        $this->__Captions = $Captions;
    } // setWidgets


    /** Title setup
     *  Set the titles for the columns. Prefix widget columns with asterisk(*)
     *  If no widget column is defined the rightmost column is considered
     *  a widget column without title. The amount of columns or widget columns
     *  is not limited
     *  @param string $Titles Semicolon separated list of titles
     *  @package solisttable
     */
    function setTitles($Titles) {
        $title_arr = soExplode($Titles, ";");
        $this->__WidgetCols = array();

        /* Init counter that count through columns */
        $ccount = 0;
        foreach($title_arr as $title) {
            if (soStrCount($title, "*")) {
                /* This is title column */
                $this->__WidgetCols[] = $ccount;

                /* Remove asterisk */
                $title = soSwapStr($title, "*", "");
                /* Set at least row change as title. Otherwise the title will
                   look very bad. Hrrr... */
                if (trim(ltrim($title)) == "") $title = "<BR>";
                $title_arr[$ccount] = $title;
            } // if soStrCount
            $ccount += 1;
        } // foreach
        /* store titles */
        $this->__Titles = $title_arr;
        /* Add column for widgets if it was not given */
        if (!count($this->__WidgetCols)) {
            $this->__Titles[] = "<BR>";
            $this->__WidgetCols[] = count($this->__Titles) - 1;
        } // if

        /* Set column count. Includes widget columns */
        $this->__ColCount = count($this->__Titles);
    } // setTitles


    /** Adds a row to the list table.
     *  Add one data row to the table. Column count must match with the amount
     *  of given titles.
     *  @param string $Values Semicolon separated list of titles
     *  @return boolean true on succees or false if amount of values did not
     *  match
     *  @package solisttable
     */
    function addRow($Values) {
        /* Html must be reconstructed -> discard old code */
        $this->__ListHtml = "";
        /* Add widget column */
        $val_arr = soExplode($Values, ";");

        /* Check that there is enough data */
        if (($this->__ColCount -count($this->__WidgetCols)) > count($val_arr)) {
            $result = false;
        } else {
            $this->__Rows->insItem($val_arr);
            $result = true;
        } // if $this->ColCount

        return $result;
    } // addRow


    /** Gets HTML code for widget.
     *  @param int $Type The widget type
     *  @param int $Row The widget column index
     *  @param int $Row The row number
     *  @param string $Caption Button caption text.
     *  @return string html code
     *  @package solisttable
     */
    function __printWidget($Type, $Col, $Row, $Caption = "") {
        $s = "<DIV ALIGN=CENTER><P>\n";
        switch ($Type) {
            case SO_LIST_TABLE_BUTTON:
                if (!$Caption) $Caption = "  O  ";
                $s .= "<INPUT TYPE=BUTTON NAME='listTable' VALUE='".
                      $Caption."'"." ONCLICK=\"soListResult.value='".
                      $Col."_".$Row."'; submit();\">\n";
                break;

            case SO_LIST_TABLE_CHECK:
                /*XXX The name is not correct */
                $s .= "<INPUT TYPE=CHECKBOX NAME='listTable' VALUE='".
                      $Row."'>";
                break;

            case SO_LIST_TABLE_RADIO:
                /*XXX The name is not correct */
                if ($this->Default == $Row)
                    $checked = " CHECKED ";
                else
                    $checked = "";
                $s .= "<INPUT TYPE=RADIO NAME='listTable' VALUE='".
                      $Row."'$checked>";
                break;
        } // switch
        $s .= "</DIV></P>\n";

        return $s;
    } // __printWidget

    /** Gets list table HTML code.
     *  @return string html code
     *  @package solisttable
     */
    function make() {
        /* Check if code is allready constructed. If code is already
           made return it. */
        if ($this->__ListHtml != "") return $this->__ListHtml; // <-- EXIT

        $th_bg_color = $this->__BgColors["th"];
        $td_bg_color = $this->__BgColors["td"];

        /* Start constructing html code */
        $s = "<INPUT TYPE=HIDDEN NAME='soListResult' VALUE=''>\n";
        if ($this->Centered)
            $s .= "<CENTER>";

        $s .="<TABLE WIDTH=".$this->Width.
             " BORDER=1 CELLPADDING=4 CELLSPACING=3>\n".
             "<TR VALIGN=TOP>\n";
        /* Print titles */
        foreach ($this->__Titles as $title) {
            $s .= "<TH bgcolor=$th_bg_color><P>".$title."</P></TH>\n";
        } // foreach
        $s .= "</TR>\n";

        /* Print rows */
        $s .= "<TBODY>\n";
        $row_count = $this->__Rows->count();
        for ($row = 0; $row < $row_count; $row++) {
            $row_data = $this->__Rows->getItem($row);
            $wid_col_count = 0;
            $s .="<TR VALIGN=TOP>\n";
            for ($c = 0; $c < $this->__ColCount; $c++) {
                $s .= "<TD bgcolor=$td_bg_color><P>\n";

                /* If a widget column, print it */
                if (($wid_col_count < count($this->__WidgetCols)) &&
                        ($c == $this->__WidgetCols[$wid_col_count])) {
                    $s .= $this->__printWidget(
                        $this->__WidgetTypes[$wid_col_count],
                        $wid_col_count +1, 
                        $row +1, 
                        $this->__Captions[$wid_col_count]);
                    $wid_col_count += 1;
                } else {
                    $s .=$row_data[$c];
                } //if

                $s .= "</P></TD>\n";
            } // for $c
            $s .= "</TR>\n";
        }// for $row
        $s .= "</TBODY>\n";
        $s .= "</TABLE>\n";

        if ($this->Centered)
            $s .= "</CENTER>\n";
        $this->__ListHtml = $s;

        return $s;
    } // make

} // class soListTable

?>
